﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SqlServer.Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /* 
         * 第一步，nuget安装 EntityFramework  6.4.4
         * 第二步，创建SqlServerDbContext和Model
         *  KeyAttribute
            StringLengthAttribute
            MaxLengthAttribute
            ConcurrencyCheckAttribute
            RequiredAttribute
            TimestampAttribute
            ComplexTypeAttribute
            ColumnAttribute
            TableAttribute
            InversePropertyAttribute
            ForeignKeyAttribute
            DatabaseGeneratedAttribute
            NotMappedAttribute
         */
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SqlServerDbContext>());/*如果模型改变，创建新的数据库*/

            SqlServerDbContext sqlServerDbContext = new SqlServerDbContext();
            //sqlServerDbContext.Database.CreateIfNotExists();
            sqlServerDbContext.Users.Add(new User { Name="winshe", Password="12345678", InsertDate=DateTime.Now });
            int count = sqlServerDbContext.SaveChanges();
            MessageBox.Show(count > 0 ? "操作成功" : "操作失败");
        }
    }
}
